package it.tref.eclipse.wicket.plugin.views;

import java.util.ArrayList;

import org.eclipse.core.runtime.IAdaptable;

public class HTMLTag implements IAdaptable
{
	private String tag, id;
	private HTMLTag parent;
	private ArrayList<HTMLTag> children;
	
	public HTMLTag(String tag) {
		super();
		this.tag = tag;
		children = new ArrayList<HTMLTag>();
	}

	public String getTag() {
		return tag;
	}
	
	public void setTag(String tag) {
		this.tag = tag;
	}
	
	public HTMLTag getParent() {
		return parent;
	}
	
	public void setParent(HTMLTag parent) {
		this.parent = parent;
	}
	
	public HTMLTag[] getChildren() {
		return (HTMLTag[])children.toArray(new HTMLTag[children.size()]);
	}
	
	public void addChild(HTMLTag child) {
		children.add(child);
		child.setParent(this);
	}
	
	public void removeChild(HTMLTag child) {
		children.remove(child);
		child.setParent(null);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object getAdapter(Class arg0) {
		return null;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}
	
	public boolean hasChildren() {
		return children.size()>0;
	}
	
	@Override
	public String toString() {
		if(id != null)
			return String.format("%s [%s]", id, tag);
		else
			return tag.toString();
	}
	
	public String getFullPath()
	{
		HTMLTag tp = parent;
		StringBuilder path = new StringBuilder();
		
		path.append(id);
		while(tp != null && tp.getId() != null)
		{
			path.append("/");
			path.append(tp.id);
			tp = tp.getParent();
		}
		
		String[]  pathArray = path.toString().split("/");
		path = new StringBuilder();
		for(int i = pathArray.length - 1; i>=0; i--)
		{
			path.append(pathArray[i]);
			if(i > 0) path.append("/");
		}
		
		return path.toString();
	}
}